<?php 

namespace app\admin\controller;
use think\Controller;
use app\admin\model\comment;
use app\admin\controller\Base;
use think\Db;
use think\Request;
class Comments extends Base{
	//判断是否是POST请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			$this->redirect('index');
		}
	}
	public function index(){
		$list = Db::table('xlblog_comment')
				->alias('a')
				->join('xlblog_blog b', 'a.ac_id = b.id')
				->order('id','dec')
				->select();
		return $this->fetch('admin/comment',['list'=>$list]);
	}
	public function delcom(){
		$this->ispost();
		$id = input('id');
		$com = comment::get($id);
		if($com->delete()){
			$data = ["result"=>1, "msg"=>"删除成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"删除失败"];
			echo json_encode($data);
		}
	}
	public function addcom(){
		$this->ispost();
		$comment = new comment;
		$comment->com_id = input('com_id');
		$comment->ac_id  = input('ac_id');
		$comment->text 	= input('text');
		$comment->name 	= session('nickname');
		$comment->time	= time();
		$comment->mail 	= session('email');
		$comment->url	= session('url');
		$comment->ip 	= session('ip');
		$comment->onemail = 'on';
		if ($comment->save()) {
			$uid = input('com_id');
			$at = Db::name('comment')->where('uid',$uid)->find();
			$weburl = request()->domain()."/article/".input('ac_id').".html";
            $con = "亲，你的回复有了新回复，详细请访问<br>".$weburl;
            sendEmail([['user_email'=>$at['mail'],'content'=>$con]]);
			$data = ["result"=>1, "msg"=>"回复成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"回复失败"];
			echo json_encode($data);
		}
	}
	public function edit(){
		$this->ispost();
		$uid = input('id');
		$com = comment::get($uid);
		$com->text = input('text');
		if ($com->save()) {
			$data = ["result"=>1, "msg"=>"编辑成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败"];
			echo json_encode($data);
		}
	}
	//修改显示状态
	public function showstatus(){
		$this->ispost();
		$id = input('id');
		$comment = comment::get($id)->toArray();
		//判断当前状态
		if ($comment['status'] == 1) {
			$comment = comment::get($id);
			$comment->status = 0;
			$comment->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}else{
			$comment = comment::get($id);
			$comment->status = 1;
			$comment->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}
	}

}

?>